﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;
using System.Resources;
using System.Collections.ObjectModel;

namespace University.Workshop.WinApp
{
    public partial class BookingRoomForm : TemplateForm
    {
        private ResourceManager resours;
        public BookingRoomForm()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(BookingRoomForm));
            try
            {
                SearchClientSimple search = new SearchClientSimple("Booking Room");
                search.ShowDialog();
                if (!string.IsNullOrEmpty(Program.ciClient))
                {
                    ciLabel.Text = Program.ciClient;
                    nombreLabel.Text = ClientManager.GetFullNameFromCI(Program.ciClient);
                    Program.ciClient = null;
                    this.bookingGroupBox.Visible = true;
                }
                else
                {
                    throw new ArgumentException("Error En la Busqueda del Cliente");

                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
                this.Close();
            }
        }
        public BookingRoomForm(DateTime checkIn, DateTime checkOut, string roomHeader)
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(BookingRoomForm));
            try
            {
                SearchClientSimple search = new SearchClientSimple("Booking Room");
                search.ShowDialog();
                if (!string.IsNullOrEmpty(Program.ciClient))
                {
                    ciLabel.Text = Program.ciClient;
                    nombreLabel.Text = ClientManager.GetFullNameFromCI(Program.ciClient);
                    Program.ciClient = null;
                    this.bookingGroupBox.Visible = true;
                }
                else
                {
                    throw new ArgumentException("Error En la Busqueda del Cliente");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            this.CheckIndateTimePicker.Value = checkIn;
            this.CheckOutdateTimePicker.Value = checkOut;
            this.GridRooms.Visible = false;
            this.bookingGroupBox.Visible = false;
            this.principalGroupBox.Visible = true;
            this.nameRoomLabel.Text = roomHeader;
            this.ControlBox = true;
        }
        private void searchbutton_Click(object sender, EventArgs e)
        {
            DateTime CheckInDate = CheckIndateTimePicker.Value.Date;
            DateTime CheckOutDate = CheckOutdateTimePicker.Value.Date;
            String type = (String)comboBoxTypes.Text.Trim();
            if (validatingForm(type,CheckInDate,CheckOutDate)) {
                int idType = RoomStuffManager.GetByName(type);
                Booking book = new Booking(CheckInDate, CheckOutDate, idType);
                Collection<Room> avilablesRooms = BookingManager.SearchByTypeAndDate(book);
                if (avilablesRooms.Count==0)
                {
                    MessageBox.Show("there is not avilable room for the type!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else {
                    GridRooms.Visible = true;
                    BookingManager.GetAllRoomsAvailable(GridRooms,avilablesRooms);
                }
            }
            else
            {
                BookingRoomForm a = new BookingRoomForm();
            }
        }
        private bool validatingForm(String type, DateTime CheckInDate, DateTime CheckOutDate) {
            if (String.IsNullOrEmpty(type))
            {
                MessageBox.Show("Select a type of Room!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!BookingManager.ValidDate(CheckInDate, CheckOutDate))
            {
                MessageBox.Show("Invlaid Dates!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;

        }

        private void GridRooms_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(GridRooms.Rows.Count - 1 > 0) && !(GridRooms.SelectedRows.Count > 0))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Invalid Selection", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                //String SelectidRoom = GridRooms.CurrentRow.Cells[0].Value.ToString();
                String SelectName = GridRooms.CurrentRow.Cells[1].Value.ToString();
                int SelectidRoom = RoomStuffManager.GetIdRoomByName(SelectName);
                Collection<Room> avilablesRooms = new Collection<Room>();
                DateTime CheckInDate = CheckIndateTimePicker.Value.Date;
                DateTime CheckOutDate = CheckOutdateTimePicker.Value.Date;
                Room roomforBooking = new Room(SelectidRoom, SelectName);
                //Booking bookingRoom = new Booking(CheckInDate,CheckOutDate,roomforBooking);
                Booking bookingRoom = new Booking { CheckIn = CheckInDate, Checkout = CheckOutDate, RoomBook = roomforBooking, ClientCI = Convert.ToInt32(ciLabel.Text) };
                if (BookingManager.BookARoom(bookingRoom))
                {
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Succeded!!", "Booking A Room", buttons, MessageBoxIcon.Exclamation);
                    GridRooms.Visible = false;
                    BookingManager.GetAllRoomsAvailable(GridRooms, avilablesRooms);
                }
                else { MessageBox.Show("Not Booking!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                
            }
        }

        private void Book_Click(object sender, EventArgs e)
        {
            try{
                String SelectName = nameRoomLabel.Text;
                int SelectidRoom = RoomStuffManager.GetIdRoomByName(SelectName);
                Collection<Room> avilablesRooms = new Collection<Room>();
                DateTime CheckInDate = CheckIndateTimePicker.Value.Date;
                DateTime CheckOutDate = CheckOutdateTimePicker.Value.Date;
                Room roomforBooking = new Room(SelectidRoom, SelectName);
               // Booking bookingRoom = new Booking(CheckInDate, CheckOutDate, roomforBooking);
               Booking bookingRoom = new Booking{ CheckIn= CheckInDate, Checkout = CheckOutDate, RoomBook= roomforBooking, ClientCI= Convert.ToInt32(ciLabel.Text) };
                if (BookingManager.BookARoom(bookingRoom))
                {
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Succeded!!", "Booking A Room", buttons, MessageBoxIcon.Exclamation);
                    GridRooms.Visible = false;
                    BookingManager.GetAllRoomsAvailable(GridRooms, avilablesRooms);
                    this.Close();
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
